<?php
            
use app\model\Util; 
use app\model\Theme; 



class Gift_Page extends PluginWebPage
{
    function main($method)
    {
        $method = 'gift_group';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $page_index = max(1,intval($_P['page']));
        $page_size = 50;
        $page_start = ($page_index-1)*$page_size;
        $list = WeikeModel::getGiftGroups($themeid,false,false);

        $has_demo_group = false;
        $res = db_find("select * from ". tablename(wk_gift_group) ." where title='演示礼物分组' and themeid='$themeid'");
        if ($res){
            $has_demo_group = true;
        }
        $return_clicent = get_defined_vars();
return $this->view('/weike/gift/main', $return_clicent);
    }

    function addpage(){
        $method = 'gift_group_add';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $group_id = intval($_P['group_id'])?:0;
        $data = [];
        if($group_id){
            $data = WeikeModel::getGiftGroupInfoById($group_id,$themeid);
        }
        $return_clicent = get_defined_vars();
return $this->view('/weike/gift/edit', $return_clicent);
    }

    function addinfo(){
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $group_id = intval($_P['group_id']);
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }
        $title = trim($_P['title'])?:'';
        $status = intval($_P['status']);
        if (empty($title)){
            show_json(0,'礼物分组名称不能为空！');
        }
        //组装数组
        $data = [
            'title'=>$title,
            'status'=>$status,
        ];
        if($group_id){
            //编辑
            //根据ID查找信息
            $info = WeikeModel::getGiftGroupInfoById($group_id,$themeid);
            if(empty($info)){
                show_json(0,'未找到礼物分组信息');
                return;
            }
            $where = ['id'=>$group_id,'themeid'=>$themeid];
            //修改数据库字段
            $res = WeikeModel::editGiftGroupDataByWhere($where,$data);
        }else{
            //新增
            //存入数据库
            $data['themeid'] = $themeid;
            $res = WeikeModel::insertGiftGroupData($data);

        }
        if (!$res){
            show_json(0,'操作失败');
        }else{
            show_json(1,'操作成功');
        }
    }

    function giftmain(){
        global $_G,$_P,$_S,$_C;
        $method = 'list';
        $do = 'weike';
        $system = $_S;
        $config = $_C;
        $group_id = intval($_P['group_id']);
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }
        //查找分组下礼品列表
        $list = WeikeModel::getGiftList($group_id,false);
        $return_clicent = get_defined_vars();
return $this->view('/weike/gift/giftmain', $return_clicent);
    }

    function addgift(){
        $method = 'add_gift';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $group_id = intval($_P['group_id'])?:0;
        if (!$group_id){
            show_json(0,'礼物分组参数错误');
            return;
        }
        $gift_id = intval($_P['gift_id']);
        $data = [];
        if($gift_id){
            $data = WeikeModel::getGiftInfoById($group_id,$gift_id);
        }
        $return_clicent = get_defined_vars();
return $this->view('/weike/gift/giftedit', $return_clicent);
    }

    function addgiftinfo(){
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $group_id = intval($_P['group_id']);
        $themeid = intval($_P['themeid']);
        $gift_id = intval($_P['gift_id'])?:0;
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }
        $title = trim($_P['title'])?:'';
        $status = intval($_P['status']);
        $price = floatval($_P['price']);
        if ($price <= 0){
            show_json(0,'价格必须大于0！');
            return;
        }
        if (empty($_P['icon'])){
            show_json(0,'请上传图标！');
            return;
        }
        if (empty($title)){
            show_json(0,'礼物名称不能为空！');
            return;
        }
        //组装数组
        $data = [
            'title'=>$title,
            'status'=>$status,
            'icon'=>tomedia($_P['icon']),
            'price'=>$price
        ];
        if($gift_id){
            //编辑
            //根据ID查找信息
            $info = WeikeModel::getGiftInfoById($group_id,$gift_id);
            if(empty($info)){
                show_json(0,'未找到礼物信息');
                return;
            }
            $where = ['id'=>$gift_id,'group_id'=>$group_id];
            //修改数据库字段
            $res = WeikeModel::editGiftDataByWhere($where,$data);
        }else{
            //新增
            //存入数据库
            $data['group_id'] = $group_id;
            $res = WeikeModel::insertGiftData($data);
        }
        if (!$res){
            show_json(0,'操作失败');
        }else{
            show_json(1,'操作成功');
        }
    }

    function listinfo(){
        $method = 'gift_list';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        $wk_id = intval($_P['wk_id']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $page_index = max(1,intval($_P['page']));
        $page_size = 50;
        $page_start = ($page_index-1)*$page_size;
        $search_data = isset($_P['search_data'])?$_P['search_data']:'';
        if ($search_data){
            $search_data = '%'.$search_data.'%';
        }
        $count = WeikeModel::getGiftRecordList($themeid,$wk_id,$search_data,0,0);
        $total = $page_total = $count['count_num'];
        if(checksubmit('export_submit', true)){
            $page_limit = 2000;
            $pagesize = ceil($total/$page_limit);
            $header = array(
                'name'=>'客户姓名','nickname' => '好友昵称','mobile' => '好友电话', 'price' => '礼物金额',
                'created_at' => '打赏时间','status' => '状态','pay_at' => '支付时间',
            );
            $keys = array_keys($header);
            $html = "\xEF\xBB\xBF";
            foreach ($header as $li) {
                $html .= $li . "\t ,";
            }
            $html .= "\n";
//            for ($j = 1; $j <= $pagesize; $j++) {
                $ex_page = $_P['ex_page']?$_P['ex_page']:1;
                $list = WeikeModel::getGiftRecordList($themeid,$wk_id,'',($ex_page-1)*5000,5000);
                if (!empty($list)) {
                    $size = ceil(count($list) / 500);
                    for ($i = 0; $i < $size; $i++) {
                        $buffer = array_slice($list, $i * 500, 500);
                        foreach ($buffer as $row) {
                            if ($row['status'] == 0){
                                $row['status'] = '未支付';
                            }elseif ($row['status'] == 1){
                                $row['status'] = '已支付';
                            }elseif ($row['status'] == 2){
                                $row['status'] = '已转账';
                            }else{
                                $row['status'] = '未知';
                            }
                            foreach ($keys as $key) {
                                $data[] = $row[$key];
                            }
                            if (isset($data)){
                                $user[] = implode("\t ,", $data) . "\t ,";
                                unset($data);
                            }
                        }
                        $html .= implode("\n", $user)."\n";
                        unset($user);
                    }
               // }
            }
            //新版本导出合并
            $is_end = 0;
            if($ex_page>=$pagesize){
                $is_end = 1;
            }
            Util::WxJump($_S,$html,'打赏礼物记录',$themeid,$is_end,$pagesize);
        }
        $list = WeikeModel::getGiftRecordList($themeid,$wk_id,$search_data,$page_start,$page_size);
        $pager = pagination($page_total,$page_index,$page_size);
        $return_clicent = get_defined_vars();
return $this->view('/weike/gift/list', $return_clicent);
    }

    function insertDemoGift()
    {
        global $_G,$_P,$_S,$_C;
        $response ='';
        $themeid  = $_P['themeid'];
        $data = [
            'title'=>'演示礼物分组',
            'status'=>1,
        ];
        $data['themeid'] = $themeid;
        $res = WeikeModel::insertGiftGroupData($data);
        if ($res){
            $group_id = $res;
            $list = [
                ['title'=>'爱你哟','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/oqttxt0ePxo6mvQddX59D505j5q9K0.png','price'=>6],
                ['title'=>'棒棒糖','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/MzjDx3DG63JdXJ3dxsxBHJ3Mld3wh6.png','price'=>88],
                ['title'=>'超爱你','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/RsJWS09OAWUQ59q521cwyS3U002uqZ.png','price'=>88],
                ['title'=>'大啤酒','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/geVEtDL26di0aEv2t9FBAaG52LL2EK.png','price'=>88],
                ['title'=>'火箭','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/XYAU31zkqJ3Alfb3aBUiuUQsJUvYZa.png','price'=>6],
                ['title'=>'集结号','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/pzxwA4xNizwtGTnijZZOJh4x467TX7.png','price'=>88],
                ['title'=>'加油鸭','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/WZ2oqOorBsaBbGRATXJ2yxYKoUd5q2.png','price'=>88],
                ['title'=>'玫瑰','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/NcSCoob85sdToS88yCWDc5SPSToCos.png','price'=>88],
                ['title'=>'你最好看','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/HHNgGG8gIvigS2V6WvhViNB4WgCnG8.png','price'=>88],
                ['title'=>'跑车','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/HgXdu77rSlaLo9g79ZLXsVn9NmlfVr.png','price'=>88],
                ['title'=>'私人飞机','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/IcdrO3c3z3Rr13OtDg131uuCuUIT1H.png','price'=>88],
                ['title'=>'送你花花','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/NnTXw5yyxyosu4XxXyxBXTVSUxoInN.png','price'=>88],
                ['title'=>'鲜花','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/nizJJnna8FJ7Dj8og88GOnI7Jg4NG8.png','price'=>88],
                ['title'=>'小心心','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/v4f58zF57i5XoaEz5fiocx55F4548N.png','price'=>88],
                ['title'=>'幸运魔盒','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/Olh6K023qiPXBkKBczcaaiKqi33Ix6.png','price'=>88],
                ['title'=>'重拳出击','status'=>1,'icon'=>'http://img2020.yixuansoft.cn/images/2/2023/02/A15De3GLPl4WH3lGPZpPpBP16ZC3Lw.png','price'=>88],
            ];
            $str = " ";
            foreach ($list as $val)
            {
                $title  = $val['title'];
                $status=1;
                $icon = $val['icon'];
                $price = $val['price'];
                $str .= "('$title','$status','$icon','$price','$group_id'),";
            }
            $str = rtrim($str,',');
            $sql = "insert into ".tablename(wk_gift)." (`title`,`status`,`icon`,`price`,`group_id`) values ".$str;
            $response = db_find($sql);
        }
        show_json(1,['msg'=>'操作成功']);
    }
}